/**
 * cookie公共方法
 */
let CookieUtil = {
    get: function(name) {
        let cookie = document.cookie,
            cookieName = encodeURIComponent(name) + "=",
            cookieStart = cookie.indexOf(cookieName),
            cookieValue = null;

        if (cookieStart > -1) {
            let cookieEnd = cookie.indexOf(";", cookieStart);
            if (cookieEnd == -1) {
                cookieEnd = cookie.length;
            }
            cookieValue = unescape(cookie.substring(cookieStart + cookieName.length, cookieEnd));
        }
        return cookieValue;
    },
    set: function(obj) {
        let params = {
            name: obj.name || "",
            value: obj.value || "",
            expires: obj.expires || "",
            path: obj.path || "",
            domain: obj.domain || "",
            secure: obj.secure || ""
        };
        let cookieText = encodeURIComponent(params.name) + "=" + encodeURIComponent(params.value);
        if (params.expires instanceof Date) {
            cookieText += "; expires=" + params.expires.toGMTString();
        }

        if (params.path) {
            cookieText += "; path=" + params.path;
        }

        if (params.domain) {
            cookieText += "; domain=" + params.domain;
        }

        if (params.secure) {
            cookieText += "; secure=";
        }
        document.cookie = cookieText;
    },
    //$.CookieUtil.del('KuGoo','/','.kugou.com',0)
    del: function(name, path, domain, secure) {
        let exp = new Date();
        exp.setTime(exp.getTime() - 1);
        let cval = this.get(name);
        this.set({
            name,
            cval,
            exp,
            path,
            domain,
            secure
        });
    },
    getValue: function(name, item) {
        let cookieArr = document.cookie.match(new RegExp("(^|\\s)" + name + "=([^;]*)", "i"));
        let cookieValue = cookieArr ? unescape(cookieArr[2]) : "";
        if (item && cookieValue) {
            let value = cookieValue.match(new RegExp("(^|\\&)" + item + "=([^\\&]*)", "i"));
            cookieValue = value ? value[2] : "";
        }
        return cookieValue;
    }
};
export default CookieUtil;
